package cn.green.core.components.store.field;

import cn.green.core.components.exceptions.FieldAuthNullException;
import cn.green.core.components.field.FieldAuthTypeCom;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className FieldAuthTypeStore
 * @description 字段权限仓库
 * @date 2024/11/18 9:13
 */
public class FieldAuthTypeStore {

    private static final Map<String, FieldAuthTypeCom> FIELD_AUTH_TYPE_MAP = new HashMap<>();
    private static final JSONArray OPTIONS = new JSONArray();

    public static void put(FieldAuthTypeCom fieldAuthTypeCom) {
        FIELD_AUTH_TYPE_MAP.put(fieldAuthTypeCom.authCode(), fieldAuthTypeCom);
        JSONObject option = new JSONObject();
        option.put("label", fieldAuthTypeCom.authName());
        option.put("value", fieldAuthTypeCom.authCode());
        OPTIONS.add(option);
    }

    public static FieldAuthTypeCom get(String authCode) {
        if (!FIELD_AUTH_TYPE_MAP.containsKey(authCode)) {
            try {
                throw new FieldAuthNullException(authCode);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

        }
        return FIELD_AUTH_TYPE_MAP.get(authCode);
    }

    public static JSONArray options() {
        return OPTIONS;
    }
}
